<template>
	<view :class="['page',{'iphoneX':isIphoneX}]">  
		<view class="body">
			<template>
				<template v-if="page=='add'">
					<view style="font-size: 32rpx;font-weight: bold;padding: 20rpx;">基本信息</view>
					<u--form :model="form" ref="uForm" class="form-wrap"> 
						<u-form-item label="单据编号" labelWidth="180" prop="receipt_no" borderBottom="true" class="u-form-item">
							<u-input v-model="form.receipt_no" disabled placeholder="输入单据编号" border="none" />
						</u-form-item> 
						<u-form-item label="单据日期" labelWidth="180" prop="receipt_date" borderBottom="true" class="u-form-item">
							<u-input v-model="form.receipt_date" disabled placeholder="输入单据日期" border="none" />
						</u-form-item> 
						<u-form-item label="业务类型" labelWidth="160" prop="operation_type_name" borderBottom="true" class="u-form-item" @click="gystypeList.isShow=true">
						 	<u-input v-model="form.operation_type_name" placeholder="选择业务类型" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
						 	<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<u-form-item label="生产部门" labelWidth="160" prop="department_name" borderBottom="true" class="u-form-item" @click="departmentList.isShow=true">
						 	<u-input v-model="form.department_name" placeholder="选择生产部门" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
						 	<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item> 
						<u-form-item label="申请人" labelWidth="180"  borderBottom="true" class="u-form-item">
							<u-input v-model="userInfo.username" disabled placeholder="输入申请人" border="none" />
						</u-form-item> 
						<u-form-item label="计划完成日期" labelWidth="240" prop="plan_finish_date" borderBottom="true" class="u-form-item" >
							<view class="form-wrap" style="width: 100%;">
								<view class="form-item" style="padding: 0;"> 
									<picker class="form-picker" mode="date" @change="changeDate(1,$event)">
										<view class="picker">
											<view class="select-data">
												<text v-if="form.plan_finish_date">{{form.plan_finish_date}}</text>
												<text v-else class="f-grey">选择计划完成日期</text>
											</view>
											<view class="icon">
												<view class="iconfont f-s34">&#xe8d4;</view>
											</view>						
										</view>	
									</picker> 						
								</view> 
							</view>			
						</u-form-item> 
					</u--form> 
					
					<view v-for="(item,index) in goods.data" :key="item.product_id" class="m-b2">
						<view class="hd-title">
							<view class="name f-wb">商品信息{{index+1}}</view>
							<view class="del-btn f-red">删除</view>
						</view>
						<u--form class="form-wrap"> 						
							<u-form-item label="商品编码" labelWidth="180" prop="p_num" borderBottom="true" class="u-form-item">
								<u-input v-model="item.p_num" placeholder="输入商品编码" border="none" />
							</u-form-item> 
							<u-form-item label="商品名称" labelWidth="180" prop="product_name" borderBottom="true" class="u-form-item">
								<u-input v-model="item.product_name" placeholder="输入商品名称" border="none" />
							</u-form-item> 
							<u-form-item label="规格型号" labelWidth="160" prop="pcategoryname" borderBottom="true" class="u-form-item">
								<u-input placeholder="选择规格型号" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item> 							
							<u-form-item label="数量" labelWidth="180" prop="fax" borderBottom="true" class="u-form-item">
								<u-input type="number" v-model="item.product_num" placeholder="输入数量" border="none" @blur="setGoodsNum(index,$event)"/>
							</u-form-item>
							<u-form-item label="单位" labelWidth="180" prop="unit_name" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.unit_name" placeholder="输入单位" border="none" />
							</u-form-item>
							<u-form-item label="存储仓库" labelWidth="160" prop="pcategoryname" borderBottom="true" class="u-form-item" @click="getStorehouse('goods',index)">
								<u-input v-model="item.storehouse_name" placeholder="选择存储仓库" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item>  
							<u-form-item label="可用库存" labelWidth="180" prop="store_count" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.store_count" disabled placeholder="可用库存" border="none" />
							</u-form-item>
							<u-form-item label="商品备注" labelWidth="180" prop="remark" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.remark" placeholder="输入商品备注" border="none" />
							</u-form-item>						
						</u--form>
					</view>
					<view class="add-btn" style="margin: 20rpx auto;" @click="selectGoods('goods')">新增商品</view>
					
					
					<template v-if="goods.data.length>0"> 
					<view v-for="(item,index) in parts.data" :key="index" class="m-b2">
						<view class="hd-title">
							<view class="name f-wb">材料信息{{index+1}}</view>
							<view class="del-btn f-red">删除</view>
						</view> 
						<u--form ref="uForm" class="form-wrap"> 										 
							<u-form-item label="选择商品名称" labelWidth="240" prop="goods_name" borderBottom="true" class="u-form-item" @click="goods.isShow=true;parts.currentIndex=index;">
								<u-input v-model="item.goods_name" placeholder="选择商品名称" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item>  
							<u-form-item label="物料编码" labelWidth="180" prop="realname" borderBottom="true" class="u-form-item">
								<u-input v-model="item.p_num" disabled placeholder="输入物料编码" border="none" />
							</u-form-item> 
							<u-form-item label="物料名称" labelWidth="180" prop="realname" borderBottom="true" class="u-form-item">
								<u-input v-model="item.product_id_name" disabled placeholder="输入物料名称" border="none" />
							</u-form-item> 							  
							<u-form-item label="规格型号" labelWidth="180" prop="mobile" borderBottom="true" class="u-form-item">
								<u-input type="text" disabled placeholder="输入规格型号" border="none" />
							</u-form-item>
							<u-form-item label="存储仓库" labelWidth="160" prop="pcategoryname" borderBottom="true" class="u-form-item" @click="getStorehouse('parts',index)">
								<u-input v-model="item.storehouse_name" placeholder="选择存储仓库" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item>  
							<u-form-item label="可用库存" labelWidth="180" prop="store_count" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.store_count" disabled placeholder="可用库存" border="none" />
							</u-form-item>
							<u-form-item label="单位" labelWidth="180" prop="uname" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.uname" disabled placeholder="输入单位" border="none" />
							</u-form-item>
							<u-form-item label="材料用量" labelWidth="180" prop="parts_use" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.parts_use" disabled placeholder="输入材料用量" border="none" />
							</u-form-item>
							<u-form-item label="产品产量" labelWidth="180" prop="product_yield" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.product_yield" disabled placeholder="输入产品产量" border="none" />
							</u-form-item>	
							<u-form-item label="损耗率" labelWidth="180" prop="attrition_rate" borderBottom="true" class="u-form-item">
								<u-input type="digit" v-model="item.attrition_rate" placeholder="输入损耗率" border="none" @blur="setAttritionNums(index)" />
							</u-form-item>
							<u-form-item label="损耗数量" labelWidth="180" prop="attrition_nums" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.attrition_nums" disabled placeholder="输入损耗数量" border="none" />
							</u-form-item> 
							<u-form-item label="发料方式" labelWidth="160" prop="pcategoryname" borderBottom="true" class="u-form-item" @click="parts.currentIndex=index;scFfTypeList.isShow=true;">
								<u-input v-model="item.distribute_parts_type_name" placeholder="选择发料方式" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item> 
							<u-form-item label="计划投料数" labelWidth="180" prop="plan_parts_nums" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.plan_parts_nums" placeholder="输入计划投料数" border="none" />
							</u-form-item>
							<u-form-item label="物料行备注" labelWidth="180" prop="remark" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.remark" placeholder="输入物料行备注" border="none" />
							</u-form-item>
						</u--form>
					</view>
					<view v-if="page=='add'" class="add-btn" style="margin: 20rpx auto;" @click="selectparts">新增材料</view>
					</template> 
					 
				</template>
				
				<template v-if="page=='detail'">
					<view style="font-size: 32rpx;font-weight: bold;padding: 20rpx;">基本信息</view>
					<u--form :model="form" ref="uForm" class="form-wrap"> 
						<u-form-item label="单据编号" labelWidth="180" prop="receipt_no" borderBottom="true" class="u-form-item">
							<u-input v-model="form.receipt_no" disabled placeholder="输入单据编号" border="none" />
						</u-form-item> 
						<u-form-item label="单据日期" labelWidth="180" prop="receipt_date" borderBottom="true" class="u-form-item">
							<u-input v-model="form.receipt_date" disabled placeholder="输入单据日期" border="none" />
						</u-form-item> 
						<u-form-item label="业务类型" labelWidth="160" prop="operation_type_name" borderBottom="true" class="u-form-item" >
						 	<u-input v-model="form.operation_type_name" placeholder="选择业务类型" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
						 	<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item>
						<u-form-item label="生产部门" labelWidth="160" prop="department_name" borderBottom="true" class="u-form-item" >
						 	<u-input v-model="form.department_name" placeholder="选择生产部门" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
						 	<u-icon	slot="right" name="arrow-right"></u-icon>						
						</u-form-item> 
						<u-form-item label="申请人" labelWidth="180"  borderBottom="true" class="u-form-item">
							<u-input v-model="form.apply_user" disabled placeholder="输入申请人" border="none" />
						</u-form-item> 
						<u-form-item label="计划完成日期" labelWidth="240" prop="plan_finish_date" borderBottom="true" class="u-form-item" >
							<view class="form-wrap" style="width: 100%;">
								<view class="form-item" style="padding: 0;"> 
									<picker class="form-picker" mode="date" >
										<view class="picker">
											<view class="select-data">
												<text v-if="form.plan_finish_date">{{form.plan_finish_date}}</text>
												<text v-else class="f-grey">选择计划完成日期</text>
											</view>
											<view class="icon">
												<view class="iconfont f-s34">&#xe8d4;</view>
											</view>						
										</view>	
									</picker> 						
								</view> 
							</view>			
						</u-form-item> 
					</u--form> 
					
					<view v-for="(item,index) in goods.data" :key="item.p_num" class="m-b2">
						<view class="hd-title">
							<view class="name f-wb">商品信息{{index+1}}</view> 
						</view>
						<u--form class="form-wrap"> 						
							<u-form-item label="商品编码" labelWidth="180" prop="p_num" borderBottom="true" class="u-form-item">
								<u-input v-model="item.p_num" disabled placeholder="输入商品编码" border="none" />
							</u-form-item> 
							<u-form-item label="商品名称" labelWidth="180" prop="product_name" borderBottom="true" class="u-form-item">
								<u-input v-model="item.product_name" disabled placeholder="输入商品名称" border="none" />
							</u-form-item> 
							<u-form-item label="规格型号" labelWidth="160" prop="pcategoryname" borderBottom="true" class="u-form-item">
								<u-input placeholder="选择规格型号" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item> 							
							<u-form-item label="数量" labelWidth="180" prop="fax" borderBottom="true" class="u-form-item">
								<u-input type="number" v-model="item.product_num" disabled placeholder="输入数量" border="none" />
							</u-form-item>
							<u-form-item label="单位" labelWidth="180" prop="unit_name" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.unit_name" disabled placeholder="输入单位" border="none" />
							</u-form-item>
							<u-form-item label="存储仓库" labelWidth="160" prop="pcategoryname" borderBottom="true" class="u-form-item" >
								<u-input v-model="item.storehouse_name" placeholder="选择存储仓库" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item>  
							<u-form-item label="可用库存" labelWidth="180" prop="store_count" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.store_count" disabled placeholder="可用库存" border="none" />
							</u-form-item>
							<u-form-item label="商品备注" labelWidth="180" prop="remark" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.remark" disabled placeholder="输入商品备注" border="none" />
							</u-form-item>						
						</u--form>
					</view>
				 
					
					
					<template v-if="goods.data.length>0"> 
					<view v-for="(item,index) in parts.data" :key="index" class="m-b2">
						<view class="hd-title">
							<view class="name f-wb">材料信息{{index+1}}</view> 
						</view> 
						<u--form ref="uForm" class="form-wrap"> 
							<template v-if="item.goods_name">
								<u-form-item label="商品名称" labelWidth="180" borderBottom="true" class="u-form-item">
									<u-input v-model="item.goods_name" disabled placeholder="输入商品名称" border="none" />
								</u-form-item> 
							</template>
							<template v-else>
								<u-form-item label="选择商品名称" labelWidth="240" prop="goods_name" borderBottom="true" class="u-form-item">
									<u-input v-model="item.goods_name" placeholder="选择商品名称" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
									<u-icon	slot="right" name="arrow-right"></u-icon>						
								</u-form-item>  
							</template>
							<u-form-item label="物料编码" labelWidth="180" prop="realname" borderBottom="true" class="u-form-item">
								<u-input v-model="item.p_num" disabled placeholder="输入物料编码" border="none" />
							</u-form-item> 
							<u-form-item label="物料名称" labelWidth="180" prop="realname" borderBottom="true" class="u-form-item">
								<u-input v-model="item.product_id_name" disabled placeholder="输入物料名称" border="none" />
							</u-form-item> 							  
							<u-form-item label="规格型号" labelWidth="180" prop="mobile" borderBottom="true" class="u-form-item">
								<u-input type="text" disabled placeholder="输入规格型号" border="none" />
							</u-form-item>
							
							<u-form-item label="可用库存" labelWidth="180" prop="store_count" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.store_count" disabled placeholder="可用库存" border="none" />
							</u-form-item>
							<u-form-item label="单位" labelWidth="180" prop="uname" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.uname" disabled placeholder="输入单位" border="none" />
							</u-form-item>
							<u-form-item label="材料用量" labelWidth="180" prop="parts_use" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.parts_use" disabled placeholder="输入材料用量" border="none" />
							</u-form-item>
							<u-form-item label="产品产量" labelWidth="180" prop="product_yield" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.product_yield" disabled placeholder="输入产品产量" border="none" />
							</u-form-item>	
							<u-form-item label="损耗率" labelWidth="180" prop="attrition_rate" borderBottom="true" class="u-form-item">
								<u-input type="digit" v-model="item.attrition_rate" placeholder="输入损耗率" border="none"/>
							</u-form-item>
							<u-form-item label="损耗数量" labelWidth="180" prop="attrition_nums" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.attrition_nums" disabled placeholder="输入损耗数量" border="none" />
							</u-form-item> 
							<u-form-item label="发料方式" labelWidth="160" prop="pcategoryname" borderBottom="true" class="u-form-item">
								<u-input v-model="item.distribute_parts_type_name" placeholder="选择发料方式" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
								<u-icon	slot="right" name="arrow-right"></u-icon>						
							</u-form-item> 
							<u-form-item label="计划投料数" labelWidth="180" prop="plan_parts_nums" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.plan_parts_nums" disabled placeholder="输入计划投料数" border="none" />
							</u-form-item>
							<u-form-item label="物料行备注" labelWidth="180" prop="remark" borderBottom="true" class="u-form-item">
								<u-input type="text" v-model="item.remark" disabled placeholder="输入物料行备注" border="none" />
							</u-form-item>
						</u--form>
					</view> 
					</template> 
					 
				</template>
				 
			
			</template>
			 
			
			<u-picker :show="gystypeList.isShow" :columns="gystypeList.data" keyName="label" :defaultIndex="gystypeList.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="gystypeList.isShow=false" @confirm="changegystypeList" @close="gystypeList.isShow=false"></u-picker>
			
			<u-picker :show="departmentList.isShow" :columns="departmentList.data" keyName="name" :defaultIndex="departmentList.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="departmentList.isShow=false" @confirm="changedelpartslist" @close="departmentList.isShow=false"></u-picker>
			
			<u-picker :show="storehouseList.isShow" :columns="storehouseList.data" keyName="sname" :defaultIndex="[]" itemHeight="90" :closeOnClickOverlay="true" @cancel="storehouseList.isShow=false" @confirm="changestorehouseList(storehouseList.type,$event)" @close="storehouseList.isShow=false"></u-picker>
			
			<u-picker :show="scFfTypeList.isShow" :columns="scFfTypeList.data" keyName="label" :defaultIndex="scFfTypeList.defaultIndex" itemHeight="90" :closeOnClickOverlay="true" @cancel="scFfTypeList.isShow=false" @confirm="changescFfTypeList" @close="scFfTypeList.isShow=false"></u-picker>
			
			<u-picker :show="goods.isShow" :columns="goods.selectData" keyName="product_name" :defaultIndex="[]" itemHeight="90" :closeOnClickOverlay="true" @cancel="goods.isShow=false" @confirm="changeGoods" @close="goods.isShow=false"></u-picker>	
			
				
			 
		</view>  
		
		<template v-if="page=='add'">
			<c-footer>
				<view slot="content" class="btn-wrap">	 
					<view class="add-btn submit-btn" @click="add">提交任务</view> 					
				</view>			
			</c-footer>
		</template>
		
		<template v-if="page=='detail'">
			<c-footer v-if="detail.status==0&&detail.approve_id.indexOf(userInfo.id)>=0">
				<view slot="content" class="btn-wrap">				 
					<template>
						<view class="add-btn submit-btn m-l2 m-r2" @click="isShowCompany=true">同意</view>
						<view class="add-btn disagree-btn m-l2 m-r2" @click="isShowReason=true">不同意</view>
					</template>		  
				</view>			
			</c-footer>
			
			<c-footer v-if="detail.status==4&&detail.approve==userInfo.department_id">
				<view slot="content" class="btn-wrap">	  
					<view class="add-btn submit-btn m-l2 m-r2" @click="lingqu">原材料领取</view>
				</view>			
			</c-footer>
			
			<c-footer v-if="detail.status==5&&detail.approve_id.indexOf(userInfo.id)>=0">
				<view slot="content" class="btn-wrap">	  
					<view class="add-btn submit-btn m-l2 m-r2" @click="finnish">已完成生产</view>
				</view>			
			</c-footer>
			
			<c-footer v-if="detail.status==6&&detail.approve_id.indexOf(userInfo.id)>=0">
				<view slot="content" class="btn-wrap">	  
					<view class="add-btn submit-btn m-l2 m-r2" @click="isCheckUser=true">指派检查人员</view>
				</view>			
			</c-footer>
			
			<c-footer v-if="detail.status==7&&detail.approve_id.indexOf(userInfo.id)>=0">
				<view slot="content" class="btn-wrap">				 
					<template>
						<view class="add-btn submit-btn m-l2 m-r2" @click="params.is_agree=1;agree()">检查通过</view>
						<view class="add-btn disagree-btn m-l2 m-r2" @click="isShowReason=true;params.is_agree=2;">检查不通过</view>
					</template>		  
				</view>			
			</c-footer>
			
			<c-footer v-if="detail.status==10&&detail.approve_id.indexOf(userInfo.id)>=0">
				<view slot="content" class="btn-wrap">				 
					<template>
						<view class="add-btn submit-btn m-l2 m-r2" @click="finished">任务已完成</view>
					</template>		  
				</view>			
			</c-footer>
		</template>	
		
		
		<u-popup :show="isShowReason" mode="center" @close="isShowReason=false;" :closeable="true">
			<view class="update-apply-reason" style="width: 450rpx;">
				<u--text text="原因" size="30" bold align="center" class="m-b2"></u--text>
				<u--form ref="uForm" class="  m-b2">
					<u--input v-model="params.reason" placeholder="请输入原因" :maxlength="300" count ></u--input>
				</u--form>
				<u-button type="primary" text="确定" size="small" @click="disagree"></u-button>
			</view>
		</u-popup>
		
		<u-popup :show="isShowCompany" mode="center" @close="isShowCompany=false;" :closeable="true">
			<view class="update-apply-reason" style="width: 450rpx;">
				<u--text text="采购公司" size="30" bold align="center" class="m-b2"></u--text>
				<u--form ref="uForm" class="  m-b2">
					<u-form-item label="" labelWidth="" prop="pcategoryname" borderBottom="true" class="u-form-item" @click="companys.isShow=true;">
						<u-input v-model="params.purchase_company_name" placeholder="选择采购公司" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
						<u-icon	slot="right" name="arrow-right"></u-icon>						
					</u-form-item>
				</u--form>
				<u-button type="primary" text="确定" size="small" @click="agree"></u-button>
			</view>
		</u-popup>
		
		<u-popup :show="isCheckUser" mode="center" @close="isCheckUser=false;" :closeable="true">
			<view class="update-apply-reason" style="width: 450rpx;">
				<u--text text="指派检查人员" size="30" bold align="center" class="m-b2"></u--text>
				<u--form ref="uForm" class="  m-b2">
					<u-form-item label="" labelWidth="" prop="pcategoryname" borderBottom="true" class="u-form-item" @click="selectUser">
						<u-input :value="params.check_userlist_name.join(',')" placeholder="选择检查人员" disabled disabledColor="#ffffff" border="none" style="pointer-events: none;"/>
						<u-icon	slot="right" name="arrow-right"></u-icon>						
					</u-form-item>
				</u--form>
				<u-button type="primary" text="确定" size="small" @click="setCheckUser"></u-button>
			</view>
		</u-popup>
		
		<u-picker :show="companys.isShow" :columns="companys.data" keyName="short_name" :defaultIndex="[]" itemHeight="90" :closeOnClickOverlay="true" @cancel="companys.isShow=false" @confirm="changeCompany" @close="companys.isShow=false"></u-picker>
	</view>
</template>

<script>
	import {dateFormat} from '@/common/util.js' 
	export default {
		data() {
			const timestamp = Date.now();
			let receipt_no = 'SC' + dateFormat(timestamp, 'YYYY-MM-DD HH:mm:ss').replace(/(-|\s|:)/g, '') + (timestamp).toString().slice(-3);
			let receipt_date=dateFormat(timestamp, 'YYYY-MM-DD')
			return {
				id:'',
				isLogin:true, 
				isIphoneX:getApp().globalData.isIphoneX,
				isAddtype:false,
				isAddUnit:false,
				systemInfo:getApp().globalData.systemInfo,
				isShowSearch:false,
				userInfo:uni.getStorageSync('userInfo'),
				type:'', 
				page:'',
				list:[], 
				detail:{},
				form:{
					id:'',
					receipt_no:receipt_no,
					receipt_date:receipt_date,
					apply_user:uni.getStorageSync('userInfo').username,
					operation_type:'',
					operation_type_name:'',
					department_id:'',
					department_name:'',
					plan_finish_date:'',  
				},
				isShowReason:false,
				isShowCompany:false,
				isCheckUser:false,
				params:{
					task_id:'',
					reason:'',
					purchase_company:'',
					purchase_company_name:'',
					check_userlist:[],
					check_userlist_name:[],
					is_agree:''
				},
				goods:{
					isShow:false,
					num:-1,
					data:[],
					currentIndex:0,
					selectNum:-1,
					selectData:[[]]
				},
				parts:{
					num:0,
					data:[],
					currentIndex:0
				},
				storehouseList:{
					type:'',
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				distribute_parts_type:{
					num:0,
					data:[[]],
					currentIndex:0
				},
				 
				gystypeList:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				scFfTypeList:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				},
				departmentList:{
					isShow:false,
					data:[[]],
					defaultIndex:[]
				}, 		
				companys:{
					isShow:false,
					data:[[]],
					defaultIndex:[] 
				}, 
				isStop:true
			}
		},
		async onLoad(params) { 
			this.type=params.type;
			this.page=params.page; 
			this.gystypeList.data[0]=this.$mock_data.scType; 
			this.scFfTypeList.data[0]=this.$mock_data.scFfType; 
			   
			let title=''; 
			if(this.page=='add'){
				title='新增任务'; 
				this.getDepartmentList();
			}
			if(this.page=='edit'){
				this.form.id=params.id;
				title='编辑任务';  
				this.getDepartmentList();
				this.setDetail();
				 
			}
			if(this.page=='detail'){
				this.form.id=params.id;
				title='任务详情';
				this.getDepartmentList();
				this.getCompany();
				this.setDetail();
			}
			
			uni.setNavigationBarTitle({
				title:title
			})	
			  
		},
		onShow() {
			
		}, 
		methods: { 
			selectUser(){ 
				uni.navigateTo({
					url:'/package_msg/mailUser/mailUser?scene=9&type=2&department_id='+(this.userInfo.department_id==29?'':this.userInfo.department_id)+'&select_data='+this.params.check_userlist.join(',')
				})
			},
			async setCheckUser(){
				if(this.params.check_userlist.length==0){
					uni.showToast({
						icon:'none',
						mask:true,
						title:'请先选择检查人员'
					})
					return
				}				
				let res=await this.$api.user_api.producecheckuser({
					task_id:this.form.id,
					check_userlist: this.params.check_userlist.join(',')
				})  
				if(res.data.code==200){
					this.setDetail();
					this.isCheckUser=false;
					this.$store.commit('shengchan_list');
				}	
			},
			async getUserList() {
				let res = await this.$api.user_api.userListBydep({ 
					keywords: '',
					department_id:''
				})     
				if (res.data.code == 200) {  
					res.data.data.forEach(item=>{						
						if(this.userInfo.department_id==item.department_id){
							item.ulist.forEach(subitem=>{
								if(subitem.dep_leader==1){	
									this.detail.apply_shenpi_user=subitem.id; 
								} 
							}) 
						} 						 
					})  
				}
			},
			
			async getCompany() {
				let res = await this.$api.user_api.companylistsel({ 
					group_id: 1
				})   
				if (res.data.code == 200) {  
					this.$set(this.companys.data,0,res.data.data); 
				}
			},
			
			changeCompany(e){  
				if(e.indexs.length==0){
					this.params.purchase_company=e.values[0][0].value;
					this.params.purchase_company_name=e.values[0][0].short_name; 
				}else{
					this.params.purchase_company=e.value[0].value;
					this.params.purchase_company_name=e.value[0].short_name; 	
				} 
				this.companys.isShow=false;
			},
			
			finnish(){
				let _this=this;
				uni.showModal({
					content:'确定已经生产完成了吗?',
					async success(m_res) {
						if(m_res.confirm){
							let res=await _this.$api.user_api.produceproduced({
								task_id: _this.form.id
							}) 
							if(res.data.code==200){
								_this.setDetail(); 
								_this.$store.commit('shengchan_list');
							}
						}
					}
				})
			},
			
			finished(){
				let _this=this;
				uni.showModal({
					content:'确定任务已完成了吗?',
					async success(m_res) {
						if(m_res.confirm){
							let res=await _this.$api.user_api.producefinished({
								task_id: _this.form.id
							}) 
							if(res.data.code==200){
								_this.setDetail(); 
								_this.$store.commit('shengchan_list');
							}
						}
					}
				})
			},
			
			lingqu(){
				let _this=this;
				uni.showModal({
					content:'确定领取原材料吗?',
					async success(m_res) {
						if(m_res.confirm){
							let res=await _this.$api.user_api.producereceive({
								task_id: _this.form.id
							}) 
							if(res.data.code==200){
								_this.setDetail(); 
								_this.$store.commit('shengchan_list');
							}
						}
					}
				})
				
			},
			
			async disagree(){
				// 生产检查
				if(this.params.is_agree==2){
					let res=await this.$api.user_api.producecheck({
						task_id:this.form.id,
						is_agree:this.params.is_agree,
						reason:this.params.reason  
					})  
					if(res.data.code==200){
						this.setDetail();
						this.isShowReason=false;
						this.params.is_agree='';
						this.$store.commit('shengchan_list');
					}
				}else{
					let res=await this.$api.user_api.producereject({
						task_id:this.form.id,
						reason:this.params.reason  
					}) 
					if(res.data.code==200){
						this.setDetail();
						this.isShowReason=false;
						this.$store.commit('shengchan_list');
					}
				}
				
				
			},
			
			async agree(){
				if(this.params.is_agree==1){
					let res=await this.$api.user_api.producecheck({
						task_id:this.form.id,
						is_agree:this.params.is_agree,
						reason:'' 
					})  
					if(res.data.code==200){
						this.setDetail();
						this.isShowReason=false;
						this.params.is_agree='';
						this.$store.commit('shengchan_list');
					}
				}else{
					if(this.companys.selectValue==''){
						uni.showToast({
							icon:'none',
							mask:true,
							title:'请先选择采购公司'
						})
						return
					}				
					let res=await this.$api.user_api.produceaccept({
						task_id:this.form.id,
						purchase_company: this.params.purchase_company
					})  
					if(res.data.code==200){
						this.setDetail();
						this.isShowCompany=false;
						this.$store.commit('shengchan_list');
					}
				}				
			},
			
			changeGoods(e){  
				this.parts.data[this.parts.currentIndex].goods_name=e.values[0][0].product_name; 
				this.parts.data[this.parts.currentIndex].goods_id=e.values[0][0].product_id;
				this.goods.isShow=false; 				
			},
			
			async setDetail(){
				let detail=await this.getproduceinfo();				
				detail.approve_id=[];
				if(detail.approve==detail.department_id){
					detail.approve_id.push(detail.approve)
				}else if(detail.approve.id){					
					detail.approve_id.push(detail.approve.id);
				}else{
					if(detail.approve&&detail.approve.length>0){
						detail.approve.forEach(i=>{
							detail.approve_id.push(i.id);
						}) 
					}
				} 
				
				this.form={
					id:detail.id,
					receipt_no:detail.receipt_no,
					receipt_date:detail.receipt_date,
					operation_type:detail.operation_type,
					apply_user:detail.apply_username,
					operation_type_name:'',
					department_id:detail.department_id,
					department_name:'',
					plan_finish_date:detail.plan_finish_date   
				}   
				this.gystypeList.data[0].forEach(j=>{
					if(detail.operation_type==j.value){
						this.form.operation_type_name=j.label;
					}
				}) 
				
				this.departmentList.data[0].forEach(j=>{
					if(detail.department_id==j.id){
						this.form.department_name=j.name;
					}
				})
				detail.goods.forEach(i=>{					
					i.storehouse=i.storehouse;
					i.storehouse_name=i.sname;
					i.store_count=i.stock;
					i.p_num=i.p_number;
				})
				this.goods.data=detail.goods;
				this.goods.num=detail.goods.length-1; 
				detail.parts.forEach(i=>{ 
					i.goods_name=i.goods_name;
					i.p_num=i.p_number; 
					i.product_id_name=i.product_name; 
					i.parts_use=Number(i.parts_use);   
					
					i.uname=i.unit_name;
					i.unit_id=i.min_unit; 
					i.store_count=i.stock;
					i.storehouse_name=i.sname; 
					this.scFfTypeList.data[0].forEach(j=>{
						if(j.value==i.distribute_parts_type){
							i.distribute_parts_type_name=j.label;
						}
					})
					
				}) 
				this.parts.data=detail.parts;
				this.parts.num=detail.parts.length-1; 				
				this.detail=detail;	 
				this.getUserList();
			},
			async getStorehouse(type,idx){	
				let item={};
				let pid='';
				this.storehouseList.type=type;
				if(type=='goods'){
					this.goods.currentIndex=idx;
					item=this.goods.data[idx]; 
					pid=item.product_id;
				}
				if(type=='parts'){
					this.parts.currentIndex=idx;
					item=this.parts.data[idx]; 
					pid=item.product_id;
				} 
				let res=await this.$api.user_api.outstock({
					pid:pid,
					unit_id:item.unit_id
				});  
				if(res.data.code==200){
					this.$set(this.storehouseList.data,0,res.data.data.stocks);  
					this.storehouseList.isShow=true;   
				} 
			},
			
			selectGoods(){				
				uni.navigateTo({
					url:'/package_kucun/list-select/list-select?page=shebei&type=goods&index='+this.goods.num
				})
			},
			
			async getDepartmentList(){
				let res=await this.$api.user_api.departmentlistsel();  
				if(res.data.code==200){ 
					this.$set(this.departmentList.data,0,res.data.data);  
				} 
			}, 
			
			changedelpartslist(e){
				this.form.department_id=e.value[0].id;
				this.form.department_name=e.value[0].name;
				this.departmentList.isShow=false; 				
			},
			
			changeDate(type,e){ 
				if(type==1){
					this.form.plan_finish_date=e.target.value;
				}
				if(type==2){
					this.form.price_validity=e.target.value;
				}
			},
			
			setGoodsNum(idx,e){				
				let item=this.goods.data[idx];  
				if(this.parts.data.length>0){
					this.parts.data.forEach(i=>{
						if(i.goods_name==item.product_name){
							i.product_yield=e;
							if(i.attrition_rate){
								i.attrition_nums=Math.floor(i.product_yield*i.attrition_rate*i.parts_use);
								i.plan_parts_nums=Number(i.product_yield*i.num)+Number(i.attrition_nums);
							}
						}
					})
				}				
			},
			
			setAttritionNums(idx){
				let item=this.parts.data[idx];
				if(item.product_yield){
					item.attrition_nums=Math.floor(item.product_yield*item.attrition_rate*item.parts_use);
					item.plan_parts_nums=Number(item.product_yield*item.parts_use)+Number(item.attrition_nums);
				}				
			},
			
			async getproducttypeparts(pid,pname,pnum){  
				let res=await this.$api.user_api.producttypeparts({
					product_id:pid
				});  
				if(res.data.code==200){ 
					if(res.data.data.length>0){
						let parts_data=[];
						res.data.data.forEach(i=>{
							let data={
								goods_name:pname,
								goods_id:pid,
								p_num:pnum,
								product_id:i.id,
								product_id_name:i.pname, 
								parts_use:Number(i.num), 
								product_yield:'', 
								attrition_rate:'', 
								attrition_nums:'',
								plan_parts_nums:'', 
								distribute_parts_type:'',
								distribute_parts_type_name:'',
								uname:i.uname,
								unit_id:i.unit_id,
								storehouse: '',
								storehouse_name:'',
								remark:'', 
							}
							parts_data.push(data)
						})
						this.parts.data=this.parts.data.concat(parts_data);
						
						this.parts.num=this.parts.data.length-1;
					}
					  
				} 
			},
			 
			  
			async getproduceinfo(){
				let res=await this.$api.user_api.produceinfo({
					id:this.form.id
				});   
				if(res.data.code==200){    
					return res.data.data; 
				} 
			},  
			changegystypeList(e){  		 
				this.form.operation_type=e.value[0].value;
				this.form.operation_type_name=e.value[0].label;
				this.gystypeList.isShow=false; 				
			},
			changestorehouseList(type,e){
				if(type=='goods'){
					this.goods.data[this.goods.currentIndex].storehouse=e.value[0].id;
					this.goods.data[this.goods.currentIndex].storehouse_name=e.value[0].sname; 
					this.goods.data[this.goods.currentIndex].store_count=e.value[0].p_count; 
				}
				if(type=='parts'){
					this.parts.data[this.parts.currentIndex].storehouse=e.value[0].id;
					this.parts.data[this.parts.currentIndex].storehouse_name=e.value[0].sname; 
					this.parts.data[this.parts.currentIndex].store_count=e.value[0].p_count; 
				} 
				this.storehouseList.isShow=false;		
			},
			changescFfTypeList(e){
				this.parts.data[this.parts.currentIndex].distribute_parts_type=e.value[0].value;
				this.parts.data[this.parts.currentIndex].distribute_parts_type_name=e.value[0].label; 
				this.scFfTypeList.isShow=false;	
			},
			  
			
			selectparts(){ 
				uni.navigateTo({
					url:'/package_kucun/list-select/list-select?page=shebei&type=parts&index='+this.parts.num
				})
			},
			  
			
			async add(){   
				let {receipt_no,receipt_date,operation_type,department_id,plan_finish_date}=this.form; 
				if(operation_type==''){
					this.$showToast({
						title:'请选择业务类型',
						duration:1000,
					})
					return
				}
				if(department_id==''){
					this.$showToast({
						title:'请选择业务部门',
						duration:1000,
					})
					return
				} 
				if(plan_finish_date==''){
					this.$showToast({
						title:'请选择计划完成时间',
						duration:1000,
					})
					return
				} 
				 
				for(let i=0;i<this.goods.data.length;i++){ 
					if(this.goods.data[i].product_num==''||this.goods.data[i].storehouse==''){
						this.$showToast({
							title:'请填写完整商品信息' 
						})
						return
					}  
				}
				for(let i=0;i<this.parts.data.length;i++){
					if(this.parts.data[i].storehouse==''||this.parts.data[i].attrition_rate==''||this.parts.data[i].plan_parts_nums==''){
						this.$showToast({
							title:'请填写完整材料信息' 
						})
						return
					}  
				}
				
				
				
				let params={receipt_no,receipt_date,operation_type,department_id,plan_finish_date};
					params.goods=JSON.stringify(this.goods.data);
					params.parts=JSON.stringify(this.parts.data);
					params.apply_username=this.form.apply_user;
					 
					 
				if(this.isStop){
					this.isStop=false
					let res=await this.$api.user_api.produceadd(params) 
					if(this.page=='add'){
						
					}  
					if(res.data.code==200){
						this.isStop=true;
						this.$showToast({
							icon:'success',
							title:res.data.msg,
							duration:1000,
						})
						this.$store.commit('shengchan_list') 
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},300)						
					}else{
						this.isStop=true;
						this.$showToast({
							icon:'none',
							title:res.data.msg,
							duration:1000,
						})
					}
				} 
				
			},
			           
			
			  
		},
		computed:{
			shebei_update(){
				return this.$store.state.shebei_update;
			},
			shebei_parts_update(){
				return this.$store.state.shebei_parts_update;
			},
			shengchan_user_jiancha(){
				return this.$store.state.shengchan_user;
			}
			
		},
		watch:{
			shebei_update(newer,older){ 
				if(newer!=older){ 
					let data={
						product_id:newer.data.id,
						product_name:newer.data.pname,
						p_num:newer.data.p_number,
						product_num:'',
						storehouse:'',
						storehouse_name:'',
						unit_name:newer.data.unit_name,		
						unit_id:newer.data.unit_id,	
						store_count:'',
						remark:'',
					}
					
					this.$set(this.goods.data,Number(newer.index)+1,data);
					this.$set(this.goods.selectData[0],Number(newer.index)+1,data); 
					this.goods.num=Number(newer.index)+1;   
					this.goods.selectNum=Number(newer.index)+1;   
					this.getproducttypeparts(newer.data.id,newer.data.pname,newer.data.p_number);
				}
			},
			shebei_parts_update(newer,older){  
				if(newer!=older){ 
					let data={
						goods_name:'',
						p_num:newer.data.p_number,
						product_id:newer.data.id,
						product_id_name:newer.data.pname, 
						parts_use:'', 
						product_yield:'', 
						attrition_rate:'', 
						attrition_nums:'',
						plan_parts_nums:'', 
						distribute_parts_type:'',
						distribute_parts_type_name:'',
						uname:newer.data.unit_name,
						unit_id:newer.data.unit_id,
						storehouse: '',
						storehouse_name:'',
						remark:'', 
					}
					this.$set(this.parts.data,Number(newer.index)+1,data);
					this.parts.num=Number(newer.index)+1;  
				} 
			},
			shengchan_user_jiancha(newer,older){
				if(newer!=older){  
					this.params.check_userlist=[];
					this.params.check_userlist_name=[];
					newer.forEach(i=>{
						this.params.check_userlist.push(i.id);
						this.params.check_userlist_name.push(i.username);
					})
				}
			}
		}
	}
</script>

<style lang="less" >
	.form-wrap{
		padding:0 20rpx; 
		.row-2{
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}
	.update-apply-reason{
		padding:30rpx;
		width: 450rpx;
	}
	.address-area{
		width: 100%;		
	}
	.btn-wrap{
		display: flex;
		justify-content: flex-end;
		align-items: center; 
	}
	.hd-title{
		display: flex;
		justify-content: space-between;
		align-items: center; 
		font-size: 32rpx; 
		padding: 20rpx;
	}
	.slot-content{
		.hd{
			padding: 30rpx 0 10rpx;
			font-size: 30rpx;
		}
	}
	.popup-form-wrap{
		padding:20rpx;
		border-radius: 15rpx;		 
	}
	.select-pz-wrap{  
		flex-grow: 2;
		.item{
			flex-grow: 2;
			display: flex; 
			align-items: center;
			margin:0 20rpx 20rpx 0;
			font-size: 30rpx;
			.del-icon{ 
			}
		}
	}
	.add-btn{
		height: 36px;
		line-height: 36px;
		width: 96%;
		margin: 5px auto;
		border:2rpx solid #09f;
		border-radius: 8rpx;
		color: #09f; 
		font-size: 30rpx;
		text-align: center;
	}  
	.submit-btn{
		background: #09f;
		color:#fff;
	}
	.disagree-btn{
		border:2rpx solid #ff9600;
		background: #ff9600;
		color:#fff;
	}
	.partslist-popup{  
		position: relative; 
		height: 100%;  
		padding: 0 40rpx; 
		.hd{  
			height: 40px; 
		}
		.select-wrap{  
			height:80%;
			flex-grow: 2; 
			overflow: hidden; 
			background: #f5f5f5;
			.scroll-view{
				height: 100%;
				overflow-y: auto; 
			}
			.item{ 
				margin-bottom: 20rpx;
			}
		}
		.btn-wrap{  
			height: 40px;
			margin: 20rpx 0;
			.btn{
				margin: 0 30rpx;
			}
		}
	}
</style>
